package com.woniuxy.service.impl;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.EquipmentDao;
import com.woniuxy.entity.Equipment;
import com.woniuxy.entity.PageBean;
import com.woniuxy.service.EquipmentService;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    EquipmentDao equipmentDao;
//通过名字条件查询设备信息分页展示
    @Override
    public PageBean<Equipment> getEquipmentPageByCondition(Equipment equipment, int pageIndex, int pageSize) {
        try {
            PageBean<Equipment> pageBean = new PageBean<>();
            //存储当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);
            //获取总条数
            int pageCount = equipmentDao.selectEquipmentCount(equipment);
            //存储总条数
            pageBean.setTotalRecord(pageCount);

            pageBean.setBeanList(equipmentDao.selectEquipmentByCondition(equipment, pageIndex, pageSize));
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }
//  通过分类查询设备信息分页展示
    @Override
    public PageBean<Equipment> getEquipmentPageByCategory(Equipment equipment, int pageIndex, int pageSize) {
        try {
            PageBean<Equipment> pageBean = new PageBean<>();
            //存储当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);
            //获取总条数
            int pageCount = equipmentDao.selectEquipmentCountByCategory(equipment);
            //存储总条数
            pageBean.setTotalRecord(pageCount);

            pageBean.setBeanList(equipmentDao.selectEquipmentByCategory(equipment, pageIndex, pageSize));
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }
    //添加设备
    @Override
    public void addEquipment(Equipment equipment,String state) {
        try {

            //通过条件查询设备
            Equipment equipments = equipmentDao.selectEquipment(equipment);

            //判断是否存在该设备
            if(equipments!=null){
                Integer equipmentId = equipments.getEquipmentId();
                //存在该设备，判断是存入还是取出
                if(state.equals("存入")){
                    //添加数据
                    int count=equipment.getCount()+equipments.getCount();
                    equipmentDao.changeCount(equipmentId, count);
                }else if(state.equals("取出")){
                    //减少数据
                    int count=equipments.getCount()-equipment.getCount();
                    if(count<0){
                     throw   new serviceException("设备数量不足");
                    }else{
                        equipmentDao.changeCount(equipmentId, count);
                    }
                }

            }else{
                //不存在，添加设备
                if(state.equals("存入")){
                    equipmentDao.appendEquipment(equipment);
                }else if(state.equals("取出")){
                    throw    new serviceException("该设备不存在");
                }

            }
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }
//获取查询到的设备
    @Override
    public List<Equipment> getEquipmentByName(Equipment equipment) {
        List<Equipment> equipments = equipmentDao.selectEquipmentByName(equipment);

        return equipments;
    }


}
